Introduction to COVID-19

image.png

Coronavirus is a family of viruses that can cause illness, which can vary from common cold and cough to sometimes more severe disease. Middle East Respiratory Syndrome (MERS-CoV) and Severe Acute Respiratory Syndrome (SARS-CoV) were such severe cases with the world already has faced. SARS-CoV-2 (n-coronavirus) is the new virus of the coronavirus family, which first discovered in 2019, which has not been identified in humans before. It is a contiguous virus which started from Wuhan in December 2019. Which later declared as Pandemic by WHO due to high rate spreads throughout the world. Currently (on date 27 March 2020), this leads to a total of 24K+ Deaths across the globe, including 16K+ deaths alone in Europe. Pandemic is spreading all over the world; it becomes more important to understand about this spread. This NoteBook is an effort to analyze the cumulative data of confirmed, deaths, and recovered cases over time. In this notebook, the main focus is to analyze the spread trend of this virus all over the world.

Downloading and Installing Prerequisites

Download Pretrained Models from GitHub:

model_confirmed.h5
model_deaths.h5
In [1]:
!pip install pycountry_convert 
!pip install folium
!wget https://raw.githubusercontent.com/tarunk04/COVID-19-CaseStudy-and-Predictions/master/models/model_deaths.h5
!wget https://raw.githubusercontent.com/tarunk04/COVID-19-CaseStudy-and-Predictions/master/models/model_confirmed.h5
!wget https://raw.githubusercontent.com/tarunk04/COVID-19-CaseStudy-and-Predictions/master/models/model_usa_c.h5
Requirement already satisfied: pycountry_convert in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (0.7.2)
Requirement already satisfied: pycountry>=16.11.27.1 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pycountry_convert) (19.8.18)
Requirement already satisfied: pytest>=3.4.0 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pycountry_convert) (5.4.1)
Requirement already satisfied: pprintpp>=0.3.0 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pycountry_convert) (0.4.0)
Requirement already satisfied: pytest-mock>=1.6.3 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pycountry_convert) (3.0.0)
Requirement already satisfied: pytest-cov>=2.5.1 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pycountry_convert) (2.8.1)
Requirement already satisfied: repoze.lru>=0.7 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pycountry_convert) (0.7)
Requirement already satisfied: wheel>=0.30.0 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pycountry_convert) (0.33.6)
Requirement already satisfied: pluggy<1.0,>=0.12 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest>=3.4.0->pycountry_convert) (0.13.1)
Requirement already satisfied: more-itertools>=4.0.0 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest>=3.4.0->pycountry_convert) (8.0.2)
Requirement already satisfied: wcwidth in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest>=3.4.0->pycountry_convert) (0.1.7)
Requirement already satisfied: atomicwrites>=1.0; sys_platform == "win32" in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest>=3.4.0->pycountry_convert) (1.3.0)
Requirement already satisfied: importlib-metadata>=0.12; python_version < "3.8" in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest>=3.4.0->pycountry_convert) (1.3.0)
Requirement already satisfied: packaging in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest>=3.4.0->pycountry_convert) (20.3)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest>=3.4.0->pycountry_convert) (0.4.3)
Requirement already satisfied: attrs>=17.4.0 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest>=3.4.0->pycountry_convert) (19.3.0)
Requirement already satisfied: py>=1.5.0 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest>=3.4.0->pycountry_convert) (1.8.1)
Requirement already satisfied: coverage>=4.4 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from pytest-cov>=2.5.1->pycountry_convert) (5.0.4)
Requirement already satisfied: zipp>=0.5 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from importlib-metadata>=0.12; python_version < "3.8"->pytest>=3.4.0->pycountry_convert) (0.6.0)
Requirement already satisfied: six in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from packaging->pytest>=3.4.0->pycountry_convert) (1.13.0)
Requirement already satisfied: pyparsing>=2.0.2 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from packaging->pytest>=3.4.0->pycountry_convert) (2.4.6)
Requirement already satisfied: folium in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (0.10.1)
Requirement already satisfied: numpy in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from folium) (1.17.4)
Requirement already satisfied: branca>=0.3.0 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from folium) (0.4.0)
Requirement already satisfied: requests in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from folium) (2.23.0)
Requirement already satisfied: jinja2>=2.9 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from folium) (2.10.3)
Requirement already satisfied: six in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from branca>=0.3.0->folium) (1.13.0)
Requirement already satisfied: chardet<4,>=3.0.2 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from requests->folium) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from requests->folium) (1.25.8)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from requests->folium) (2019.11.28)
Requirement already satisfied: idna<3,>=2.5 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from requests->folium) (2.9)
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from jinja2>=2.9->folium) (1.1.1)
'wget' is not recognized as an internal or external command,
operable program or batch file.
'wget' is not recognized as an internal or external command,
operable program or batch file.
'wget' is not recognized as an internal or external command,
operable program or batch file.

Importing python libraries and datasets

In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from matplotlib import ticker
import pycountry_convert as pc
import folium
!pip install branca
import branca
Requirement already satisfied: branca in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (0.4.0)
Requirement already satisfied: jinja2 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from branca) (2.10.3)
Requirement already satisfied: six in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from branca) (1.13.0)
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from jinja2->branca) (1.1.1)
In [3]:
from datetime import datetime, timedelta, date
from scipy.interpolate import make_interp_spline, BSpline
!pip install plotly
import plotly.express as px
import json, requests
Requirement already satisfied: plotly in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (4.6.0)
Requirement already satisfied: six in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from plotly) (1.13.0)
Requirement already satisfied: retrying>=1.3.3 in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from plotly) (1.3.3)
In [5]:
from keras.layers import Input, Dense, Activation, LeakyReLU
from keras import models
from keras.optimizers import RMSprop, Adam
Using TensorFlow backend.
In [6]:
df_confirmed = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv')
df_confirmed.head(5)
Out[6]:
Province/State Country/Region Lat Long 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 ... 3/23/20 3/24/20 3/25/20 3/26/20 3/27/20 3/28/20 3/29/20 3/30/20 3/31/20 4/1/20
0 NaN Afghanistan 33.0000 65.0000 0 0 0 0 0 0 ... 40 74 84 94 110 110 120 170 174 237
1 NaN Albania 41.1533 20.1683 0 0 0 0 0 0 ... 104 123 146 174 186 197 212 223 243 259
2 NaN Algeria 28.0339 1.6596 0 0 0 0 0 0 ... 230 264 302 367 409 454 511 584 716 847
3 NaN Andorra 42.5063 1.5218 0 0 0 0 0 0 ... 133 164 188 224 267 308 334 370 376 390
4 NaN Angola -11.2027 17.8739 0 0 0 0 0 0 ... 3 3 3 4 4 5 7 7 7 8

5 rows × 75 columns

In [10]:
df_deaths = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv')
print('Time series of Covid-19 Deaths: \n')
df_deaths.head(5)
Time series of Covid-19 Deaths: 

Out[10]:
Province/State Country/Region Lat Long 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 ... 3/23/20 3/24/20 3/25/20 3/26/20 3/27/20 3/28/20 3/29/20 3/30/20 3/31/20 4/1/20
0 NaN Afghanistan 33.0000 65.0000 0 0 0 0 0 0 ... 1 1 2 4 4 4 4 4 4 4
1 NaN Albania 41.1533 20.1683 0 0 0 0 0 0 ... 4 5 5 6 8 10 10 11 15 15
2 NaN Algeria 28.0339 1.6596 0 0 0 0 0 0 ... 17 19 21 25 26 29 31 35 44 58
3 NaN Andorra 42.5063 1.5218 0 0 0 0 0 0 ... 1 1 1 3 3 3 6 8 12 14
4 NaN Angola -11.2027 17.8739 0 0 0 0 0 0 ... 0 0 0 0 0 0 2 2 2 2

5 rows × 75 columns

In [9]:
df_covid19 = pd.read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/web-data/data/cases_country.csv")
print('Covid 19 Dataset: \n')
df_covid19.head()
Covid 19 Dataset: 

Out[9]:
Country_Region Last_Update Lat Long_ Confirmed Deaths Recovered Active
0 Australia 2020-04-02 02:53:53 -25.0000 133.0000 4862 20 422 4420
1 Austria 2020-04-02 02:47:12 47.5162 14.5501 10711 146 1436 9129
2 Canada 2020-04-02 02:53:39 60.0010 -95.0010 9731 112 1435 0
3 China 2020-04-02 01:38:02 30.5928 114.3055 82392 3316 76420 2656
4 Denmark 2020-04-02 02:47:12 56.0000 10.0000 3290 104 971 2215
In [11]:
df_table = pd.read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/web-data/data/cases_time.csv",parse_dates=['Last_Update'])
df_table.head()
Out[11]:
Country_Region Last_Update Confirmed Deaths Recovered Active Delta_Confirmed Delta_Recovered
0 Afghanistan 2020-01-22 0 0 NaN NaN 0 NaN
1 Afghanistan 2020-01-23 0 0 NaN NaN 0 NaN
2 Afghanistan 2020-01-24 0 0 NaN NaN 0 NaN
3 Afghanistan 2020-01-25 0 0 NaN NaN 0 NaN
4 Afghanistan 2020-01-26 0 0 NaN NaN 0 NaN
In [12]:
df_table = df_table[df_table["Country_Region"] != "Others"]

Finding country wise information

In [13]:
df_countries_cases = df_covid19.copy().drop(['Lat','Long_','Last_Update'],axis =1)
In [14]:
df_countries_cases.index = df_countries_cases["Country_Region"]
df_countries_cases= df_countries_cases.drop("Country_Region", axis=1)
df_countries_cases.head()
Out[14]:
Confirmed Deaths Recovered Active
Country_Region
Australia 4862 20 422 4420
Austria 10711 146 1436 9129
Canada 9731 112 1435 0
China 82392 3316 76420 2656
Denmark 3290 104 971 2215

Global cases reported till Date

Total number of Confirmed, Deaths, Recovered and Active cases across the globe

In [15]:
globalcases_df = pd.DataFrame(df_countries_cases.sum()).transpose().style.background_gradient(cmap='Wistia',axis=1)
globalcases_df
Out[15]:
Confirmed Deaths Recovered Active
0 937091 47231 193764 485109

Country Wise Reported Cases

Country Wise reported confirmed cases, recovered cases, deaths, active cases

In [16]:
df_countries_cases.sort_values('Confirmed', ascending= False).style.background_gradient(cmap='Wistia')
Out[16]:
Confirmed Deaths Recovered Active
Country_Region
US 216515 5119 8593 0
Italy 110574 13155 16847 80572
Spain 104118 9387 22647 72084
China 82392 3316 76420 2656
Germany 77981 931 18700 58350
France 57763 4043 11055 42665
Iran 47593 3036 15473 29084
United Kingdom 29865 2357 179 27329
Switzerland 17768 488 2967 14313
Turkey 15679 277 333 15069
Belgium 13964 828 2132 11004
Netherlands 13696 1175 260 12261
Austria 10711 146 1436 9129
Korea, South 9976 169 5828 3979
Canada 9731 112 1435 0
Portugal 8251 187 43 8021
Brazil 6931 244 127 6560
Israel 6092 26 241 5825
Sweden 4947 239 103 4605
Norway 4877 44 13 4820
Australia 4862 20 422 4420
Czechia 3589 39 61 3489
Ireland 3447 85 5 3357
Denmark 3290 104 971 2215
Chile 3031 16 234 2781
Malaysia 2908 45 645 2218
Russia 2777 24 190 2563
Ecuador 2758 98 58 2602
Poland 2554 43 56 2455
Romania 2460 92 252 2116
Japan 2384 57 472 1855
Luxembourg 2319 29 80 2210
Philippines 2311 96 50 2165
Pakistan 2118 27 94 1997
India 1998 58 148 1792
Thailand 1771 12 505 1254
Saudi Arabia 1720 16 264 1440
Indonesia 1677 157 103 1417
Finland 1446 17 10 1419
Greece 1415 51 52 1312
South Africa 1380 5 50 1325
Mexico 1378 37 35 1306
Peru 1323 47 394 882
Panama 1317 32 9 1276
Dominican Republic 1284 57 9 1218
Iceland 1220 2 236 982
Argentina 1133 33 248 852
Colombia 1065 17 39 1009
Serbia 1060 28 0 1032
Singapore 1000 4 245 751
Croatia 963 6 73 884
Algeria 847 58 61 728
Slovenia 841 15 10 816
Qatar 835 2 71 762
United Arab Emirates 814 8 61 745
New Zealand 797 1 92 704
Ukraine 794 20 13 761
Estonia 779 5 33 741
Egypt 779 52 179 548
Iraq 728 52 182 494
Diamond Princess 712 11 619 82
Morocco 654 39 29 586
Lithuania 581 8 7 566
Armenia 571 4 31 536
Bahrain 569 4 337 228
Hungary 525 20 40 465
Lebanon 479 14 43 422
Bosnia and Herzegovina 459 13 19 427
Latvia 446 0 1 445
Moldova 423 5 23 395
Tunisia 423 12 5 406
Bulgaria 422 10 20 392
Slovakia 400 1 3 396
Andorra 390 14 10 366
Kazakhstan 380 3 26 351
Costa Rica 375 2 4 369
Azerbaijan 359 5 26 328
North Macedonia 354 11 17 326
Uruguay 350 2 62 286
Taiwan* 329 5 39 285
Cyprus 320 9 28 283
Kuwait 317 0 80 237
Burkina Faso 282 16 46 220
Jordan 278 5 36 237
Albania 259 15 67 177
Afghanistan 237 4 5 228
San Marino 236 28 13 195
Cameroon 233 6 10 217
Vietnam 222 0 63 159
Cuba 212 6 12 194
Oman 210 1 34 175
Ghana 195 5 31 159
Senegal 190 1 45 144
Cote d'Ivoire 190 1 9 180
Malta 188 0 2 186
Uzbekistan 181 2 12 167
Nigeria 174 2 9 163
Honduras 172 10 3 159
Belarus 163 2 53 108
Mauritius 161 6 0 155
Sri Lanka 146 3 21 122
Venezuela 144 3 43 98
West Bank and Gaza 134 1 18 115
Brunei 131 1 52 78
Kosovo 125 1 10 114
Montenegro 123 2 0 121
Georgia 117 0 23 94
Bolivia 115 7 1 107
Kyrgyzstan 111 0 3 108
Cambodia 110 0 34 76
Congo (Kinshasa) 109 9 3 97
Trinidad and Tobago 90 5 1 84
Rwanda 82 0 0 82
Kenya 81 1 3 77
Paraguay 77 3 2 72
Niger 74 5 0 69
Liechtenstein 72 0 0 72
Madagascar 57 0 0 57
Monaco 55 1 2 52
Bangladesh 54 6 25 23
Guatemala 46 1 12 33
Barbados 45 0 0 45
Uganda 44 0 0 44
Jamaica 44 3 2 39
Zambia 36 0 0 36
Togo 36 2 10 24
El Salvador 33 2 0 31
Djibouti 33 0 0 33
Mali 31 3 0 28
Guinea 30 0 0 30
Ethiopia 29 0 2 27
Congo (Brazzaville) 22 2 0 20
Bahamas 21 1 1 19
Tanzania 20 1 1 18
Guyana 19 4 0 15
Maldives 19 0 13 6
Gabon 18 1 0 17
Burma 16 1 0 15
Haiti 16 0 1 15
Eritrea 15 0 0 15
Equatorial Guinea 15 0 1 14
Mongolia 14 0 2 12
Namibia 14 0 2 12
Saint Lucia 13 0 1 12
Benin 13 0 1 12
Dominica 12 0 0 12
Syria 10 2 0 8
Suriname 10 0 0 10
Grenada 10 0 0 10
Seychelles 10 0 0 10
Laos 10 0 0 10
Libya 10 0 0 10
Mozambique 10 0 0 10
Eswatini 9 0 0 9
Guinea-Bissau 9 0 0 9
MS Zaandam 9 2 0 7
Angola 8 2 1 5
Saint Kitts and Nevis 8 0 0 8
Zimbabwe 8 1 0 7
Sudan 7 2 2 3
Antigua and Barbuda 7 0 0 7
Chad 7 0 0 7
Liberia 6 0 0 6
Holy See 6 0 0 6
Mauritania 6 1 2 3
Cabo Verde 6 1 0 5
Nicaragua 5 1 0 4
Nepal 5 0 1 4
Somalia 5 0 1 4
Fiji 5 0 0 5
Bhutan 5 0 1 4
Botswana 4 1 0 3
Gambia 4 1 2 1
Central African Republic 3 0 0 3
Belize 3 0 0 3
Sierra Leone 2 0 0 2
Burundi 2 0 0 2
Saint Vincent and the Grenadines 2 0 1 1
Timor-Leste 1 0 0 1
Papua New Guinea 1 0 0 1

Top 10 countries with Confirmed, Deaths, Recovered, Active cases

In [17]:
f = plt.figure(figsize=(10,5))
f.add_subplot(111)

plt.axes(axisbelow=True)
plt.barh(df_countries_cases.sort_values('Confirmed')["Confirmed"].index[-10:],df_countries_cases.sort_values('Confirmed')["Confirmed"].values[-10:],color="darkcyan")
plt.tick_params(size=5,labelsize = 13)
plt.xlabel("Confirmed Cases",fontsize=18)
plt.title("Top 10 Countries (Confirmed Cases)",fontsize=20)
plt.grid(alpha=0.3)
#plt.savefig(out+'Top 10 Countries (Confirmed Cases).png')
In [18]:
f = plt.figure(figsize=(10,5))
f.add_subplot(111)

plt.axes(axisbelow=True)
plt.barh(df_countries_cases.sort_values('Deaths')["Deaths"].index[-10:],df_countries_cases.sort_values('Deaths')["Deaths"].values[-10:],color="brown")
plt.tick_params(size=5,labelsize = 13)
plt.xlabel("Death Cases",fontsize=18)
plt.title("Top 10 Countries (Death Cases)",fontsize=20)
plt.grid(alpha=0.3)
#plt.savefig(out+'Top 10 Countries (Confirmed Cases).png')
In [19]:
f = plt.figure(figsize=(10,5))
f.add_subplot(111)

plt.axes(axisbelow=True)
plt.barh(df_countries_cases.sort_values('Recovered')["Recovered"].index[-10:],df_countries_cases.sort_values('Recovered')["Recovered"].values[-10:],color="green")
plt.tick_params(size=5,labelsize = 13)
plt.xlabel("Recovered Cases",fontsize=18)
plt.title("Top 10 Countries (Recovered Cases)",fontsize=20)
plt.grid(alpha=0.3)
#plt.savefig(out+'Top 10 Countries (Confirmed Cases).png')
In [20]:
f = plt.figure(figsize=(10,5))
f.add_subplot(111)

plt.axes(axisbelow=True)
plt.barh(df_countries_cases.sort_values('Active')["Active"].index[-10:],df_countries_cases.sort_values('Active')["Active"].values[-10:],color="orange")
plt.tick_params(size=5,labelsize = 13)
plt.xlabel("Active Cases",fontsize=18)
plt.title("Top 10 Countries (Active Cases)",fontsize=20)
plt.grid(alpha=0.3)
#plt.savefig(out+'Top 10 Countries (Confirmed Cases).png')

Correlation Analysis using Heatmap

Heat map on correlation of confirmed, recovered, deaths and active COVID-19 cases

In [21]:
df_countries_cases.corr().style.background_gradient(cmap='Reds')
Out[21]:
Confirmed Deaths Recovered Active
Confirmed 1 0.788443 0.57601 0.619826
Deaths 0.788443 1 0.521151 0.8422
Recovered 0.57601 0.521151 1 0.417125
Active 0.619826 0.8422 0.417125 1

Visualization on world map using Folium

In [22]:
world_map = folium.Map(location=[10,0], tiles="cartodbpositron", zoom_start=2,max_zoom=6,min_zoom=2)
for i in range(0,len(df_confirmed)):
    folium.Circle(
        location=[df_confirmed.iloc[i]['Lat'], df_confirmed.iloc[i]['Long']],
        tooltip = "<h5 style='text-align:center;font-weight: bold'>"+df_confirmed.iloc[i]['Country/Region']+"</h5>"+
                    "<div style='text-align:center;'>"+str(np.nan_to_num(df_confirmed.iloc[i]['Province/State']))+"</div>"+
                    "<hr style='margin:10px;'>"+
                    "<ul style='color: #444;list-style-type:circle;align-item:left;padding-left:20px;padding-right:20px'>"+
        "<li>Confirmed: "+str(df_confirmed.iloc[i,-1])+"</li>"+
        "<li>Deaths:   "+str(df_deaths.iloc[i,-1])+"</li>"+
        "<li>Mortality Rate:   "+str(np.round(df_deaths.iloc[i,-1]/(df_confirmed.iloc[i,-1]+1.00001)*100,2))+"</li>"+
        "</ul>"
        ,
        radius=(int((np.log(df_confirmed.iloc[i,-1]+1.00001)))+0.2)*50000,
        color='#ff6600',
        fill_color='#ff8533',
        fill=True).add_to(world_map)

world_map
Out[22]:

Progression of COVID spreading worldwide

In [23]:
df_data = df_table.groupby(['Last_Update', 'Country_Region'])['Confirmed', 'Deaths'].max().reset_index()
df_data["Last_Update"] = pd.to_datetime( df_data["Last_Update"]).dt.strftime('%m/%d/%Y')

fig = px.scatter_geo(df_data, locations="Country_Region", locationmode='country names', 
                     color=np.power(df_data["Confirmed"],0.3)-2 , size= np.power(df_data["Confirmed"]+1,0.3)-1, hover_name="Country_Region",
                     hover_data=["Confirmed"],
                     range_color= [0, max(np.power(df_data["Confirmed"],0.3))], 
                     projection="natural earth", animation_frame="Last_Update", 
                     color_continuous_scale=px.colors.sequential.Plasma,
                     title='COVID-19: Progression of spreading worldwide'
                    )
fig.update_coloraxes(colorscale="hot")
fig.update(layout_coloraxis_showscale=False)
fig.show()

Severity of COVID-19 across globe using Heatmap

In [34]:
temp_df = pd.DataFrame(df_countries_cases['Confirmed'])
temp_df = temp_df.reset_index()
fig = px.choropleth(temp_df, locations="Country_Region",
                    color=np.log10(temp_df.iloc[:,-1]), # lifeExp is a column of gapminder
                    hover_name="Country_Region", # column to add to hover information
                    hover_data=["Confirmed"],
                    color_continuous_scale=px.colors.sequential.Plasma,locationmode="country names")
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(title_text="Confirmed Cases across globe")
fig.update_coloraxes(colorbar_title="Confirmed Cases across globe",colorscale="Reds")
# fig.to_image("Global Heat Map confirmed.png")
fig.show()
In [33]:
temp_df = pd.DataFrame(df_countries_cases['Deaths'])
temp_df = temp_df.reset_index()
fig = px.choropleth(temp_df, locations="Country_Region",
                    color=np.log10(temp_df.iloc[:,-1]), # lifeExp is a column of gapminder
                    hover_name="Country_Region", # column to add to hover information
                    hover_data=["Deaths"],
                    color_continuous_scale=px.colors.sequential.Plasma,locationmode="country names")
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(title_text="Death Cases across globe")
fig.update_coloraxes(colorbar_title="Death Cases across globe",colorscale="Reds")
# fig.to_image("Global Heat Map confirmed.png")
fig.show()

COVID-19 : INDIA

image.png

Dataset:

This dataset is provided by API for COVID-19 stats in India, sourced from The Ministry of Health and Family Welfare and separately from unofficial sources https://api.rootnet.in/

In [36]:
df_covid19_india_test = pd.io.json.json_normalize(requests.get('https://api.rootnet.in/covid19-in/stats/testing/history').json()['data']).rename(columns = {"totalIndividualsTested":"covid_individualtested","totalPositiveCases":"corona_positives","totalSamplesTested":"totalsamplestested"})
df_covid19_india_test.head()
Out[36]:
day totalsamplestested covid_individualtested corona_positives source
0 2020-03-12 6500 5900 78 Press_Release_ICMR_13March2020.pdf
1 2020-03-18 13125 12235 150 ICMR_website_update_18March_6PM_IST.pdf
2 2020-03-19 14175 13285 182 ICMR_website_update_19March_6PM_IST.pdf
3 2020-03-20 15404 14514 236 ICMR_website_update_20March_6PM_IST.pdf
4 2020-03-21 16911 16021 315 ICMR_website_update_21March_6PM_IST.pdf
In [37]:
df_covid19_india_test["pt_ratio"]= np.round(100*df_covid19_india_test["corona_positives"]/df_covid19_india_test["totalsamplestested"],2)
df_covid19_india_test["positive"] = df_covid19_india_test["corona_positives"].diff()
df_covid19_india_test["tests"] = df_covid19_india_test["totalsamplestested"].diff()
df_covid19_india_test["pt_ratio"]= np.round(100*df_covid19_india_test["positive"]/df_covid19_india_test["tests"],2)
In [42]:
df_covid19_india_test.tail()
Out[42]:
day totalsamplestested covid_individualtested corona_positives source pt_ratio positive tests
8 2020-03-25 22928 22038 539 ICMR_website_update_25March_10AM_IST.pdf 1.28 3.0 234.0
9 2020-03-27 27688 26798 691 https://icmr.nic.in/sites/default/files/whats_... 3.19 152.0 4760.0
10 2020-03-30 38442 37000 1334 https://www.youtube.com/watch?v=yvJY0LuM8kQ&fe... 5.98 643.0 10754.0
11 2020-03-31 42788 41000 1533 MOH announcement 4.58 199.0 4346.0
12 2020-04-01 47951 46000 1637 MOH announcement 2.01 104.0 5163.0
In [40]:
f = plt.figure(figsize=(10,5))
f.add_subplot(111)

plt.axes(axisbelow=True)
plt.bar(df_covid19_india_test["day"],df_covid19_india_test["tests"].values[:],color="Green",label="Test (-ve)"+str())
plt.bar(df_covid19_india_test["day"],df_covid19_india_test["positive"].values[:],bottom=df_covid19_india_test["tests"].values[:]-df_covid19_india_test["positive"].values[:],color="Red",label="Test (+ve)")
plt.tick_params(size=5,labelsize = 13)
plt.tick_params(axis="x",size=5,labelsize = 13,labelrotation=90 )
plt.xlabel("Date",fontsize=18)
plt.ylabel("Test Results",fontsize=18)

plt.title("Tests in India",fontsize=20)
plt.grid(alpha=0.3)
plt.legend()
#plt.savefig(out+'Test_India.png')
Out[40]:
<matplotlib.legend.Legend at 0x1d8a38ac308>

Total samples tested till April 1,2020

In [41]:
df_covid19_india_test["totalsamplestested"][-1:].values[0]
Out[41]:
47951

COVID19 Cases in India

In [47]:
india_data_json = requests.get('https://api.rootnet.in/covid19-in/unofficial/covid19india.org/statewise').json()
df_india_cases = pd.io.json.json_normalize(india_data_json['data']['statewise'])
df_india_cases = df_india_cases.set_index("state")
df_india_cases.head()
Out[47]:
confirmed recovered deaths active
state
Maharashtra 338 39 14 285
Kerala 265 26 2 237
Tamil Nadu 234 6 1 227
Delhi 152 6 2 144
Rajasthan 129 3 0 126
In [48]:
total = df_india_cases.sum()
total.name = "Total"
pd.DataFrame(total).transpose().style.background_gradient(cmap='Wistia',axis=1)
Out[48]:
confirmed recovered deaths active
Total 2094 171 57 1866
In [49]:
total
Out[49]:
confirmed    2094
recovered     171
deaths         57
active       1866
Name: Total, dtype: int64
In [51]:
df_india_cases.style.background_gradient(cmap='Wistia')
Out[51]:
confirmed recovered deaths active
state
Maharashtra 338 39 14 285
Kerala 265 26 2 237
Tamil Nadu 234 6 1 227
Delhi 152 6 2 144
Rajasthan 129 3 0 126
Uttar Pradesh 117 17 2 98
Andhra Pradesh 132 2 0 130
Karnataka 110 9 3 98
Telangana 127 14 6 107
Gujarat 87 7 7 73
Madhya Pradesh 98 0 6 92
Jammu and Kashmir 62 2 2 58
Punjab 46 1 4 41
Haryana 43 27 0 16
West Bengal 37 3 6 28
Bihar 24 0 1 23
Chandigarh 17 0 0 17
Ladakh 13 3 0 10
Assam 16 0 0 16
Andaman and Nicobar Islands 10 0 0 10
Chhattisgarh 9 2 0 7
Uttarakhand 7 2 0 5
Goa 5 0 0 5
Odisha 5 1 0 4
Himachal Pradesh 3 1 1 1
Puducherry 3 0 0 3
Jharkhand 1 0 0 1
Manipur 2 0 0 2
Mizoram 1 0 0 1
Arunachal Pradesh 1 0 0 1
Dadra and Nagar Haveli 0 0 0 0
Daman and Diu 0 0 0 0
Lakshadweep 0 0 0 0
Meghalaya 0 0 0 0
Nagaland 0 0 0 0
Sikkim 0 0 0 0
Tripura 0 0 0 0

States with reported deaths

In [52]:
df_india_cases[df_india_cases["deaths"] > 0].style.background_gradient(cmap = 'Wistia')
Out[52]:
confirmed recovered deaths active
state
Maharashtra 338 39 14 285
Kerala 265 26 2 237
Tamil Nadu 234 6 1 227
Delhi 152 6 2 144
Uttar Pradesh 117 17 2 98
Karnataka 110 9 3 98
Telangana 127 14 6 107
Gujarat 87 7 7 73
Madhya Pradesh 98 0 6 92
Jammu and Kashmir 62 2 2 58
Punjab 46 1 4 41
West Bengal 37 3 6 28
Bihar 24 0 1 23
Himachal Pradesh 3 1 1 1

Top 10 states in India affected by COVID-19

States with Confirmed cases

In [64]:
f = plt.figure(figsize=(10,5))
f.add_subplot(111)

plt.axes(axisbelow=True)
plt.barh(df_india_cases.sort_values('confirmed')["confirmed"].index[-10:],df_india_cases.sort_values('confirmed')["confirmed"].values[-10:],color="orange")
plt.tick_params(size=5,labelsize = 13)
plt.xlabel("Confirmed Cases",fontsize=18)
plt.title("Top 10 States in India (Confirmed Cases)",fontsize=20)
plt.grid(alpha=0.3)
#plt.savefig(out+'Top 10 States_India (Confirmed Cases).png')

States with Death cases

In [65]:
f = plt.figure(figsize=(10,5))
f.add_subplot(111)

plt.axes(axisbelow=True)
plt.barh(df_india_cases.sort_values('deaths')["deaths"].index[-10:],df_india_cases.sort_values('deaths')["deaths"].values[-10:],color="brown")
plt.tick_params(size=5,labelsize = 13)
plt.xlabel("Death Cases",fontsize=18)
plt.title("Top 10 States in India (Deaths Cases)",fontsize=20)
plt.grid(alpha=0.3)
#plt.savefig(out+'Top 10 States_India (Confirmed Cases).png')

Correlation using Heatmap

In [67]:
df_india_cases.corr().style.background_gradient(cmap='Reds')
Out[67]:
confirmed recovered deaths active
confirmed 1 0.759522 0.64602 0.995955
recovered 0.759522 1 0.609017 0.701158
deaths 0.64602 0.609017 1 0.606348
active 0.995955 0.701158 0.606348 1
In [78]:
# Adding states geolocation(Latitude,Longitude) data to India dataset
geolocations = {
    "Kerala" : [10.8505,76.2711],
    "Maharashtra" : [19.7515,75.7139],
    "Karnataka": [15.3173,75.7139],
    "Telangana": [18.1124,79.0193],
    "Uttar Pradesh": [26.8467,80.9462],
    "Rajasthan": [27.0238,74.2179],
    "Gujarat":[22.2587,71.1924],
    "Delhi" : [28.7041,77.1025],
    "Punjab":[31.1471,75.3412],
    "Tamil Nadu": [11.1271,78.6569],
    "Haryana": [29.0588,76.0856],
    "Madhya Pradesh":[22.9734,78.6569],
    "Jammu and Kashmir":[33.7782,76.5762],
    "Ladakh": [34.1526,77.5770],
    "Andhra Pradesh":[15.9129,79.7400],
    "West Bengal": [22.9868,87.8550],
    "Bihar": [25.0961,85.3131],
    "Chhattisgarh":[21.2787,81.8661],
    "Chandigarh":[30.7333,76.7794],
    "Uttarakhand":[30.0668,79.0193],
    "Himachal Pradesh":[31.1048,77.1734],
    "Goa": [15.2993,74.1240],
    "Odisha":[20.9517,85.0985],
    "Andaman and Nicobar Islands": [11.7401,92.6586],
    "Puducherry":[11.9416,79.8083],
    "Manipur":[24.6637,93.9063],
    "Mizoram":[23.1645,92.9376],
    "Assam":[26.2006,92.9376],
    "Meghalaya":[25.4670,91.3662],
    "Tripura":[23.9408,91.9882],
    "Arunachal Pradesh":[28.2180,94.7278],
    "Jharkhand" : [23.6102,85.2799],
    "Nagaland": [26.1584,94.5624],
    "Sikkim": [27.5330,88.5122],
    "Dadra and Nagar Haveli":[20.1809,73.0169],
    "Lakshadweep":[10.5667,72.6417],
    "Daman and Diu":[20.4283,72.8397]    
}
df_india_cases["Lat"] = ""
df_india_cases["Long"] = ""
for index in df_india_cases.index :
    df_india_cases.loc[df_india_cases.index == index,"Lat"] = geolocations[index][0]
    df_india_cases.loc[df_india_cases.index == index,"Long"] = geolocations[index][1]

Visualization using Folium map for statewise

In [90]:
# url = "https://raw.githubusercontent.com/Subhash9325/GeoJson-Data-of-Indian-States/master/Indian_States"
# state_json = requests.get(url).json()
india = folium.Map(location=[23,80], zoom_start=4,max_zoom=10, tiles='OpenStreetMap', attr= "INDIA" ,min_zoom=4,height=500,width="80%")
for i in range(0,len(df_india_cases[df_india_cases['confirmed']>0].index)):
    folium.Circle(
        location=[df_india_cases.iloc[i]['Lat'], df_india_cases.iloc[i]['Long']],
        tooltip = "<h5 style='text-align:center;font-weight: bold'>"+df_india_cases.iloc[i].name+"</h5>"+
                    "<hr style='margin:10px;'>"+
                    "<ul style='color: #444;list-style-type:circle;align-item:left;padding-left:20px;padding-right:20px'>"+
        "<li>Confirmed: "+str(df_india_cases.iloc[i]['confirmed'])+"</li>"+
        "<li>Active:   "+str(df_india_cases.iloc[i]['active'])+"</li>"+
        "<li>Recovered:   "+str(df_india_cases.iloc[i]['recovered'])+"</li>"+
        "<li>Deaths:   "+str(df_india_cases.iloc[i]['deaths'])+"</li>"+
        
        "<li>Mortality Rate:   "+str(np.round(df_india_cases.iloc[i]['deaths']/(df_india_cases.iloc[i]['confirmed']+1)*100,2))+"</li>"+
        "</ul>"
        ,
        radius=(int(np.log2(df_india_cases.iloc[i]['confirmed']+1)))*15000,
        color='#ff6600',
        fill_color='#ff8533',
        fill=True).add_to(india)

india
Out[90]:

COVID-19 US image.png

Load US dataset

In [92]:
df_temp = pd.read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/03-31-2020.csv")
df_usa_cases = df_temp.loc[df_temp["Country_Region"]== "US"]
df_usa_cases = df_usa_cases.rename(columns={"Admin2":"County"})
df_usa_cases.head()
Out[92]:
FIPS County Province_State Country_Region Last_Update Lat Long_ Confirmed Deaths Recovered Active Combined_Key
0 45001.0 Abbeville South Carolina US 2020-03-31 23:43:56 34.223334 -82.461707 4 0 0 0 Abbeville, South Carolina, US
1 22001.0 Acadia Louisiana US 2020-03-31 23:43:56 30.295065 -92.414197 39 1 0 0 Acadia, Louisiana, US
2 51001.0 Accomack Virginia US 2020-03-31 23:43:56 37.767072 -75.632346 7 0 0 0 Accomack, Virginia, US
3 16001.0 Ada Idaho US 2020-03-31 23:43:56 43.452658 -116.241552 195 3 0 0 Ada, Idaho, US
4 19001.0 Adair Iowa US 2020-03-31 23:43:56 41.330756 -94.471059 1 0 0 0 Adair, Iowa, US
In [93]:
total = df_usa_cases.sum()
total
Out[93]:
FIPS                                                    6.36648e+07
Province_State    South CarolinaLouisianaVirginiaIdahoIowaMissou...
Country_Region    USUSUSUSUSUSUSUSUSUSUSUSUSUSUSUSUSUSUSUSUSUSUS...
Last_Update       2020-03-31 23:43:562020-03-31 23:43:562020-03-...
Lat                                                         81041.2
Long_                                                       -193009
Confirmed                                                    188172
Deaths                                                         3873
Recovered                                                      7024
Active                                                            0
Combined_Key      Abbeville, South Carolina, USAcadia, Louisiana...
dtype: object
In [94]:
total.name = "Total"
pd.DataFrame(total).transpose().loc[:,["Confirmed","Deaths"]].style.background_gradient(cmap='Wistia',axis=1)
Out[94]:
Confirmed Deaths
Total 188172 3873
In [95]:
df_usa_cases.loc[:,["Confirmed","Deaths","Province_State"]].groupby(["Province_State"]).sum().sort_values("Confirmed",ascending=False).style.background_gradient(cmap='Wistia')
Out[95]:
Confirmed Deaths
Province_State
New York 75833 1550
New Jersey 18696 267
California 8210 173
Michigan 7615 259
Florida 6741 85
Massachusetts 6620 89
Illinois 5994 99
Washington 5432 225
Louisiana 5237 239
Pennsylvania 4963 63
Georgia 3929 111
Texas 3809 54
Connecticut 3128 69
Colorado 2966 69
Tennessee 2391 23
Ohio 2199 55
Indiana 2158 49
Maryland 1660 18
North Carolina 1535 12
Wisconsin 1412 25
Missouri 1357 15
Arizona 1289 24
Virginia 1249 27
Nevada 1114 26
South Carolina 1083 22
Alabama 987 23
Mississippi 937 20
Utah 888 5
Oregon 690 18
Minnesota 629 12
Kentucky 628 11
Oklahoma 568 23
Arkansas 523 8
Idaho 515 8
Iowa 497 7
District of Columbia 495 9
Rhode Island 488 8
Kansas 434 9
New Hampshire 357 3
Delaware 319 10
New Mexico 315 4
Maine 303 5
Vermont 293 13
Puerto Rico 239 8
Hawaii 204 0
Montana 198 5
Nebraska 172 3
West Virginia 162 1
North Dakota 122 3
Alaska 119 3
Wyoming 109 0
South Dakota 108 1
Grand Princess 103 3
Guam 69 2
Diamond Princess 49 0
Virgin Islands 30 0
Northern Mariana Islands 2 0
Recovered 0 0

Top 15 affected US Cities

In [96]:
df_usa_cases.loc[:,["Province_State","Confirmed","Deaths","County"]].sort_values("Confirmed",ascending=False).set_index("County")[:15].style.background_gradient(cmap='Wistia')
Out[96]:
Province_State Confirmed Deaths
County
New York City New York 43119 932
Westchester New York 9967 10
Nassau New York 8544 48
Suffolk New York 6713 44
Cook Illinois 4496 61
Wayne Michigan 3735 120
Unassigned New Jersey 3686 247
Los Angeles California 3019 54
Bergen New Jersey 2909 4
Rockland New York 2863 8
King Washington 2330 150
Miami-Dade Florida 2123 7
Essex New Jersey 1900 4
Fairfield Connecticut 1870 38
Orleans Louisiana 1834 101

Most affected states in US with COVID cases

Confirm cases

In [97]:
f = plt.figure(figsize=(10,5))
f.add_subplot(111)

plt.axes(axisbelow=True)
plt.barh(df_usa_cases.groupby(["Province_State"]).sum().sort_values('Confirmed')["Confirmed"].index[-10:],df_usa_cases.groupby(["Province_State"]).sum().sort_values('Confirmed')["Confirmed"].values[-10:],color="orange")
plt.tick_params(size=5,labelsize = 13)
plt.xlabel("Confirmed Cases",fontsize=18)
plt.title("Top 10 States in US: (Confirmed Cases)",fontsize=20)
plt.grid(alpha=0.3)
#plt.savefig(out+'Top 10 States in US (Confirmed Cases).png')

Death cases

In [98]:
f.add_subplot(111)

plt.axes(axisbelow=True)
plt.barh(df_usa_cases.groupby(["Province_State"]).sum().sort_values('Deaths')["Deaths"].index[-10:],df_usa_cases.groupby(["Province_State"]).sum().sort_values('Deaths')["Deaths"].values[-10:],color="brown")
plt.tick_params(size=5,labelsize = 13)
plt.xlabel("Death Cases",fontsize=18)
plt.title("Top 10 States in US: (Death Cases)",fontsize=20)
plt.grid(alpha=0.3)
#plt.savefig(out+'Top 10 States in US (Death Cases).png')
In [99]:
# url = "https://raw.githubusercontent.com/Subhash9325/GeoJson-Data-of-Indian-States/master/Indian_States"
# state_json = requests.get(url).json()
df_usa_cases = df_usa_cases.replace(np.nan, 0, regex=True)
usa = folium.Map(location=[37, -102], zoom_start=4,max_zoom=8,min_zoom=4)
for i in np.int32(np.asarray(df_usa_cases[df_usa_cases['Confirmed'] > 0].index)):
    folium.Circle(
        location=[df_usa_cases.loc[i]['Lat'], df_usa_cases.loc[i]['Long_']],
        tooltip = "<h5 style='text-align:center;font-weight: bold'>"+df_usa_cases.loc[i]['Province_State']+"</h5>"+
                    "<div style='text-align:center;'>"+str(np.nan_to_num(df_usa_cases.loc[i]['County']))+"</div>"+
                    "<hr style='margin:10px;'>"+
                    "<ul style='color: #444;list-style-type:circle;align-item:left;padding-left:20px;padding-right:20px'>"+
        "<li>Confirmed: "+str(df_usa_cases.loc[i]['Confirmed'])+"</li>"+
        "<li>Active:   "+str(df_usa_cases.loc[i]['Active'])+"</li>"+
        "<li>Recovered:   "+str(df_usa_cases.loc[i]['Recovered'])+"</li>"+       
        "<li>Deaths:   "+str(df_usa_cases.loc[i]['Deaths'])+"</li>"+
        "<li>Mortality Rate:   "+str(np.round(df_usa_cases.loc[i]['Deaths']/(df_usa_cases.loc[i]['Confirmed']+1)*100,2))+"</li>"+
        "</ul>"
        ,
        radius=(int(np.log2(df_usa_cases.loc[i]['Confirmed']+1)))*6000,
        color='#ff6600',
        fill_color='#ff8533',
        fill=True).add_to(usa)

usa
Out[99]:
In [103]:
data_y = np.log10(np.asarray(df_confirmed.sum()[5:]).astype("float32"))
data_x = np.arange(1,len(data_y)+1)

Graph for COVID-19 cases all over the world

In [135]:
def plot_params(ax,axis_label= None, plt_title = None,label_size=15, axis_fsize = 15, title_fsize = 20, scale = 'linear' ):
    # Tick-Parameters
    ax.xaxis.set_minor_locator(ticker.AutoMinorLocator())
    ax.yaxis.set_minor_locator(ticker.AutoMinorLocator())
    ax.tick_params(which='both', width=1,labelsize=label_size)
    ax.tick_params(which='major', length=6)
    ax.tick_params(which='minor', length=3, color='0.8')
    
    # Grid
    plt.grid(lw = 1, ls = '-', c = "0.7", which = 'major')
    plt.grid(lw = 1, ls = '-', c = "0.9", which = 'minor')

    # Plot Title
    plt.title( plt_title,{'fontsize':title_fsize})
    
    # Yaxis sacle
    plt.yscale(scale)
    plt.minorticks_on()
    # Plot Axes Labels
    xl = plt.xlabel(axis_label[0],fontsize = axis_fsize)
    yl = plt.ylabel(axis_label[1],fontsize = axis_fsize)
In [139]:
def visualize_covid_cases(confirmed, deaths, continent=None , country = None , state = None, period = None, figure = None, scale = "linear"):
    x = 0
    if figure == None:
        f = plt.figure(figsize=(10,10))
        # Sub plot
        ax = f.add_subplot(111)
    else :
        f = figure[0]
        # Sub plot
        ax = f.add_subplot(figure[1],figure[2],figure[3])
    
    plt.tight_layout(pad=10, w_pad=5, h_pad=5)
    
    stats = [confirmed, deaths]
    label = ["Confirmed", "Deaths"]
    
    if continent != None:
        params = ["continent",continent]
    elif country != None:
        params = ["country",country]
    else: 
        params = ["All", "All"]
    color = ["darkcyan","crimson"]
    marker_style = dict(linewidth=3, linestyle='-', marker='o',markersize=4, markerfacecolor='#ffffff')
    for i,stat in enumerate(stats):
        if params[1] == "All" :
            cases = np.sum(np.asarray(stat.iloc[:,5:]),axis = 0)[x:]
        else :
            cases = np.sum(np.asarray(stat[stat[params[0]] == params[1]].iloc[:,5:]),axis = 0)[x:]
        date = np.arange(1,cases.shape[0]+1)[x:]
        plt.plot(date,cases,label = label[i]+" (Total : "+str(cases[-1])+")",color=color[i],**marker_style)

    if params[1] == "All" :
        Total_confirmed = np.sum(np.asarray(stats[0].iloc[:,5:]),axis = 0)[x:]
        Total_deaths = np.sum(np.asarray(stats[1].iloc[:,5:]),axis = 0)[x:]
    else :
        Total_confirmed =  np.sum(np.asarray(stats[0][stat[params[0]] == params[1]].iloc[:,5:]),axis = 0)[x:]
        Total_deaths = np.sum(np.asarray(stats[1][stat[params[0]] == params[1]].iloc[:,5:]),axis = 0)[x:]
        
    text = "From "+stats[0].columns[5]+" to "+stats[0].columns[-1]+"\n"
    text += "Mortality rate : "+ str(int(Total_deaths[-1]/(Total_confirmed[-1])*10000)/100)+"\n"
    text += "Last 5 Days:\n"
    text += "Confirmed : " + str(Total_confirmed[-1] - Total_confirmed[-6])+"\n"
    text += "Deaths : " + str(Total_deaths[-1] - Total_deaths[-6])+"\n"
    text += "Last 24 Hours:\n"
    text += "Confirmed : " + str(Total_confirmed[-1] - Total_confirmed[-2])+"\n"
    text += "Deaths : " + str(Total_deaths[-1] - Total_deaths[-2])+"\n"
    
    plt.text(0.02, 0.78, text, fontsize=15, horizontalalignment='left', verticalalignment='top', transform=ax.transAxes,bbox=dict(facecolor='white', alpha=0.4))
    
    # Plot Axes Labels
    axis_label = ["Days ("+df_confirmed.columns[5]+" - "+df_confirmed.columns[-1]+")","No of Cases"]
    
    # Plot Parameters
    plot_params(ax,axis_label,scale = scale)
    
    # Plot Title
    if params[1] == "All" :
        plt.title("COVID-19 Cases World",{'fontsize':25})
    else:   
        plt.title("COVID-19 Cases for "+params[1] ,{'fontsize':25})
        
    # Legend Location
    l = plt.legend(loc= "best",fontsize = 15)
    
    if figure == None:
        plt.show()
In [137]:
cols = 1
rows = 1
f = plt.figure(figsize=(10,10*rows))
visualize_covid_cases(df_confirmed, df_deaths,continent = "All",figure = [f,rows,cols, 1])

#plt.savefig(out+'COIVD-19-World.png')
plt.show()

Loading Pre trained Model (model_confirmed.h5)

In [101]:
model = models.load_model("model_confirmed.h5")
model.summary()
WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:174: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:181: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:186: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:190: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:199: The name tf.is_variable_initialized is deprecated. Please use tf.compat.v1.is_variable_initialized instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:206: The name tf.variables_initializer is deprecated. Please use tf.compat.v1.variables_initializer instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:986: The name tf.assign_add is deprecated. Please use tf.compat.v1.assign_add instead.

WARNING:tensorflow:From C:\Users\Surya-Rekha\Anaconda3\envs\myenv\lib\site-packages\keras\backend\tensorflow_backend.py:973: The name tf.assign is deprecated. Please use tf.compat.v1.assign instead.

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_19 (InputLayer)        (None, 1)                 0         
_________________________________________________________________
Dense_l1 (Dense)             (None, 80)                160       
_________________________________________________________________
LRelu_l1 (LeakyReLU)         (None, 80)                0         
_________________________________________________________________
Dense_l2 (Dense)             (None, 80)                6480      
_________________________________________________________________
LRelu_l2 (LeakyReLU)         (None, 80)                0         
_________________________________________________________________
Dense_l3 (Dense)             (None, 1)                 81        
_________________________________________________________________
Output (LeakyReLU)           (None, 1)                 0         
=================================================================
Total params: 6,721
Trainable params: 6,721
Non-trainable params: 0
_________________________________________________________________
In [141]:
model2 = models.load_model("model_deaths.h5")
model2.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_18 (InputLayer)        (None, 1)                 0         
_________________________________________________________________
Dense_l1 (Dense)             (None, 80)                160       
_________________________________________________________________
LRelu_l1 (LeakyReLU)         (None, 80)                0         
_________________________________________________________________
Dense_l2 (Dense)             (None, 80)                6480      
_________________________________________________________________
LRelu_l2 (LeakyReLU)         (None, 80)                0         
_________________________________________________________________
Dense_l3 (Dense)             (None, 1)                 81        
_________________________________________________________________
Output (LeakyReLU)           (None, 1)                 0         
=================================================================
Total params: 6,721
Trainable params: 6,721
Non-trainable params: 0
_________________________________________________________________

Predictions for COVID-19 cases

1. Prediction cases for COVID-19 globally

Using Moving Average and Exponential Weighted Moving Average models

In [149]:
def cal_moving_avg(input_df, window=2):
    """Calculates Moving avg using the window size."""
    _df = input_df.rolling(window, axis=1).median()['4/1/20']
    
    return _df

def cal_ewma(input_df, comm=0.3):
    """Calculates the exp weighted moving average using the window size."""
    _df = input_df.ewm(com=comm).mean()['4/1/20']
    
    return _df
In [151]:
all_pred_df = pd.DataFrame()
all_pred_df_ew = pd.DataFrame()

for df in [df_confirmed, df_deaths]:
    _df = cal_moving_avg(df, window=2)
    ew_df = cal_ewma(df, comm=0.4)
    all_pred_df = pd.concat([all_pred_df, _df], axis=1)
    all_pred_df_ew = pd.concat([all_pred_df_ew, ew_df], axis=1)
    
all_pred_df.columns =['Confirmed', 'Deaths']    
all_pred_df_ew.columns =['Confirmed', 'Deaths']    
In [173]:
print('\n')
print('COVID-19 cases worldwide till 1/4/2020 using Moving Average:')
all_pred_df

COVID-19 cases worldwide till 1/4/2020 using Moving Average:
Out[173]:
Confirmed Deaths
0 205.5 4.0
1 251.0 15.0
2 781.5 51.0
3 383.0 13.0
4 7.5 2.0
... ... ...
251 5.5 0.0
252 5.5 1.0
253 4.0 1.0
254 2.0 0.0
255 1.5 0.0

256 rows × 2 columns

In [174]:
print('\n')
print('COVID-19 cases worldwide till 1/4/2020 using EWMA:')
all_pred_df_ew

COVID-19 cases worldwide till 1/4/2020 using EWMA:
Out[174]:
Confirmed Deaths
0 237.000000 4.000000
1 254.111111 12.555556
2 687.716418 45.791045
3 473.635220 22.930818
4 140.404173 7.951714
... ... ...
251 5.869350 0.021517
252 8.105529 1.434719
253 5.173008 1.124206
254 2.906574 0.321202
255 2.259021 0.091772

256 rows × 2 columns

2. Prediction cases for COVID-19 in India

In [176]:
ind_conf = df_confirmed[df_confirmed['Country/Region'] == 'India']
ind_conf
Out[176]:
Province/State Country/Region Lat Long 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 ... 3/23/20 3/24/20 3/25/20 3/26/20 3/27/20 3/28/20 3/29/20 3/30/20 3/31/20 4/1/20
131 NaN India 21.0 78.0 0 0 0 0 0 0 ... 499 536 657 727 887 987 1024 1251 1397 1998

1 rows × 75 columns

In [177]:
ind_deaths = df_deaths[df_deaths['Country/Region'] == 'India']
ind_deaths
Out[177]:
Province/State Country/Region Lat Long 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 ... 3/23/20 3/24/20 3/25/20 3/26/20 3/27/20 3/28/20 3/29/20 3/30/20 3/31/20 4/1/20
131 NaN India 21.0 78.0 0 0 0 0 0 0 ... 10 10 12 20 20 24 27 32 35 58

1 rows × 75 columns

Use Moving Average and EWMA models¶

In [175]:
def cal_moving_avg(input_df, window=2):
    """Calculates Moving avg using the window size."""
    _df = input_df.rolling(window, axis=1).median()['4/1/20']
    
    return _df

def cal_ewma(input_df, comm=0.3):
    """Calculates the exp weighted moving average using the window size."""
    _df = input_df.ewm(com=comm).mean()['4/1/20']
    
    return _df
In [179]:
ind_all_pred_df = pd.DataFrame()
ind_all_pred_df_ew = pd.DataFrame()

for df in [ind_conf, ind_deaths]:
    _df = cal_moving_avg(df, window=2)
    ew_df = cal_ewma(df, comm=0.4)
    ind_all_pred_df = pd.concat([ind_all_pred_df, _df], axis=1)
    ind_all_pred_df_ew = pd.concat([ind_all_pred_df_ew, ew_df], axis=1)
    
ind_all_pred_df.columns =['Confirmed', 'Deaths']    
ind_all_pred_df_ew.columns =['Confirmed', 'Deaths'] 
In [180]:
print('\n')
print('COVID-19 cases in India till 1/4/2020 using MA:')
ind_all_pred_df

COVID-19 cases in India till 1/4/2020 using MA:
Out[180]:
Confirmed Deaths
131 1697.5 46.5
In [181]:
print('\n')
print('COVID-19 cases in US till 1/4/2020 using EWMA:')
ind_all_pred_df_ew

COVID-19 cases in US till 1/4/2020 using EWMA:
Out[181]:
Confirmed Deaths
131 1998.0 58.0

3. Prediction cases for COVID-19 in US

In [168]:
us_conf = df_confirmed[df_confirmed['Country/Region'] == 'US']
us_conf
Out[168]:
Province/State Country/Region Lat Long 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 ... 3/23/20 3/24/20 3/25/20 3/26/20 3/27/20 3/28/20 3/29/20 3/30/20 3/31/20 4/1/20
225 NaN US 37.0902 -95.7129 1 1 2 2 5 5 ... 43847 53740 65778 83836 101657 121478 140886 161807 188172 213372

1 rows × 75 columns

In [169]:
us_deaths = df_deaths[df_deaths['Country/Region'] == 'US']
us_deaths
Out[169]:
Province/State Country/Region Lat Long 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 ... 3/23/20 3/24/20 3/25/20 3/26/20 3/27/20 3/28/20 3/29/20 3/30/20 3/31/20 4/1/20
225 NaN US 37.0902 -95.7129 0 0 0 0 0 0 ... 557 706 942 1209 1581 2026 2467 2978 3873 4757

1 rows × 75 columns

Use Moving Average and EWMA models

In [163]:
def cal_moving_avg(input_df, window=2):
    """Calculates Moving avg using the window size."""
    _df = input_df.rolling(window, axis=1).median()['4/1/20']
    
    return _df

def cal_ewma(input_df, comm=0.3):
    """Calculates the exp wighted moving average using the window size."""
    _df = input_df.ewm(com=comm).mean()['4/1/20']
    
    return _df
In [170]:
us_all_pred_df = pd.DataFrame()
us_all_pred_df_ew = pd.DataFrame()

for df in [us_conf, us_deaths]:
    _df = cal_moving_avg(df, window=2)
    ew_df = cal_ewma(df, comm=0.4)
    us_all_pred_df = pd.concat([us_all_pred_df, _df], axis=1)
    us_all_pred_df_ew = pd.concat([us_all_pred_df_ew, ew_df], axis=1)
    
us_all_pred_df.columns =['Confirmed', 'Deaths']    
us_all_pred_df_ew.columns =['Confirmed', 'Deaths']    
In [171]:
print('\n')
print('COVID-19 cases in US till 1/4/2020 using MA:')
us_all_pred_df

COVID-19 cases in US till 1/4/2020 using MA:
Out[171]:
Confirmed Deaths
225 200772.0 4315.0
In [172]:
print('\n')
print('COVID-19 cases in US till 1/4/2020 using EWMA:')
us_all_pred_df_ew

COVID-19 cases in US till 1/4/2020 using EWMA:
Out[172]:
Confirmed Deaths
225 213372.0 4757.0

Save the results into excel file

In [189]:
!pip install openpyxl
all_pred_df.to_excel("COVID19_Global_Cases.xlsx", index = False)
Requirement already satisfied: openpyxl in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (3.0.3)
Requirement already satisfied: et-xmlfile in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from openpyxl) (1.0.1)
Requirement already satisfied: jdcal in c:\users\surya-rekha\anaconda3\envs\myenv\lib\site-packages (from openpyxl) (1.4.1)
In [ ]: